#include "AccountManager.h"

#include <Network/Client.h>

using namespace Account;

AccountManager::AccountManager(Network::Client &client)
	: client(client), logged_in(false)
{
	login_events.func_event("Connection-Accepted").set(this, &AccountManager::onEventConnectionAccepted);
	login_events.func_event("Connection-Disconnected").set(this, &AccountManager::onEventConnectionDisconnected);
	login_events.func_event("Login-Success").set(this, &AccountManager::onEventLoginSuccess);
	login_events.func_event("Login-Fail").set(this, &AccountManager::onEventLoginFail);
}

AccountManager::~AccountManager()
{
}

void AccountManager::update(const unsigned int &pulse_ms)
{
	//Update game
	float dt = (float)pulse_ms / 1000.0f;
}

void AccountManager::onEventConnectionAccepted(const CL_NetGameEvent &e)
{
	cl_log_event("Account", "Connection Accepted by Server");

	//Just redirect to login request immediatly for now...
	client.sendEvent(CL_NetGameEvent("Login", "my user name"));
}

void AccountManager::onEventConnectionDisconnected(const CL_NetGameEvent &e)
{
	cl_log_event("Account", "Connection Disconnected by Server");
	logged_in = false;
}

void AccountManager::onEventLoginSuccess(const CL_NetGameEvent &e)
{
	cl_log_event("Account", "Login success");

	logged_in = true;

	client.sendEvent(CL_NetGameEvent("Game-RequestStart"));
}

void AccountManager::onEventLoginFail(const CL_NetGameEvent &e)
{
	CL_String fail_reason = e.get_argument(0);

	cl_log_event("Account", "Login failed: %1", fail_reason);
}
